export default {
	async goNext() {
		let rule = [{
			name: 'tel',
			label: '手机号',
			rule: ['validateTel_']
		}, {
			name: 'code',
			label: '验证码',
			rule: ['validateCode']
		}, {
			name: 'pwd',
			label: '密码',
			rule: ['validatePwd']
		}, {
			name: 'confirmPwd',
			label: '确认密码',
			rule: ['validatePwd']
		}]
		if(!this.validatetool.validate(this, this.params, rule)) {
			return;
		}
		if(this.currentType != 'personRegister'){
			if(!this.params.realName){
				this.$message.error('请输入企业全称');
				return false;
			}
		}
		this.params.userName = this.params.realName;
		if(this.params.pwd != this.params.confirmPwd) {
			this.$message.error('两次密码输入不一致');
			return false;
		}

		let params = {
			phone: this.params.tel,
			code: this.params.code,
			userType: this.currentType
		}
		let res = await this.httptool.post(this.apis.checkCode, params);
		debugger
		if(res.code==1){
			if(this.currentType == 'personRegister') {
				this.personRegister();
			} else {
				this.stepOne = false;
			}
		}
		console.log(this.stepOne);
		
	},
	async personRegister() {
		let params = {
			tel: this.params.tel,
			code: this.params.code,
			pwd: this.params.pwd,
			confirmPwd: this.params.confirmPwd
		}
		let res = await this.httptool.post(this.apis.customerPersonRegister, params);
				this.stepOne=false;
				this.success=true;
//				this.$message.success('注册成功!');
//				this.$router.push('/price');

	},
	async getCustomerTree() {
		let res = await this.httptool.post(this.apis.customerTree, null);
		this.tableData = this.changeChildren(res.data);
	},
	changeChildren(arr) {
		if(!arr) {
			return null;
		}
		if(arr && arr.length == 0) {
			return null;
		}
		for(let i = 0; i < arr.length; i++) {
			if(arr.children && arr.children.length === 0) {
				arr[i].children = null;
			} else {
				arr[i].children = this.changeChildren(arr[i].children)
			}
		}
		return arr;
	},
	async getCategoryTree() {
		let res = await this.httptool.post(this.apis.categoryTree, null);
		this.tableData = res.data;
	},
	async regist() {
		//买家调会员注册，卖家调贸易商注册
		let requestUrl = ''; //会员注册
		if(this.currentType == 'customer') {
			requestUrl = 'customer/register';
		} else if(this.currentType == 'trade') {
			requestUrl = 'trade/register';
		}
		let rule = [{
			name: 'productCategoryIds',
			label: '类别',
			rule: ['validateSelectEmpty_']
		}, {
			name: 'realName',
			label: '企业全称',
			rule: ['validateEmpty_']
		}, {
			name: 'shortName',
			label: '企业简称',
			rule: ['validateEmpty_']
		}, {
			name: 'organCode',
			label: '企业统一社会信用代码',
			rule: ['validateSocialCreditCode']
		}, {
			name: 'contactName',
			label: '法人姓名',
			rule: ['validateEmpty_']
		}, {
			name: 'idNo',
			label: '法人身份证',
			rule: ['validateID_']
		}, {
			name: 'contactTel',
			label: '联系人电话',
			rule: ['validateTel_']
		}, {
			name: 'addressPre',
			label: '地址',
			rule: ['validateSelectEmpty_']
		}, {
			name: 'addressDetail',
			label: '详细地址',
			rule: ['validateEmpty_']
		}, {
			name: 'businessLicense',
			label: '营业执照',
			rule: ['validateUploadEmpty_']
		}]
		if(!this.validatetool.validate(this, this.params, rule)) {
			return;
		}
		this.params.userName = this.params.realName;
		let res = await this.httptool.post(requestUrl,  this.params);
		this.stepOne = null;
		this.success = true;
		//		this.$message('提交成功，请耐心等待平台审核！');
		//		this.$router.push('/price');

	},
	async lognin() {
		var telReg = /^[1][3,4,5,7,8][0-9]{9}$/;
		if(!this.loginForm.tel) {
			this.$message('手机号不能为空');
			return false
		} else if(!telReg.test(this.loginForm.tel)) {
			this.$message('手机号格式不正确')
			return false;
		} else if(!this.loginForm.pwd) {
			this.$message('密码不能为空');
			return false
		}
		if(this.currentType != 'personRegister'){
			this.loginForm.bos=false;
			if(!this.loginForm.realName){
				this.$message('请先选择企业');
				return false;
			}			
		}else{
			this.loginForm.bos=true;
		}
		let res = await this.httptool.post(this.apis.login, this.loginForm);
		this.$message.success('登录成功');
		
		this.tool.setToken(res.data.token);
		this.$store.commit('changeValue', {
			name: 'userInfo',
			value: res.data.customer
		});
		if(this.from == null || this.from == '/') {
			this.$router.replace('/price');
		} else if(this.from == '/price' ){//发标
			if(this.$route.query && Object.keys(this.$route.query).length>0 ){
				this.$router.replace(JSON.parse(this.$route.query.url));
			}else{
				this.$router.replace('/price');
			}
			
		} else if(this.from != '/forgetpwd' && this.from != '/regist') {
			this.$router.replace(this.from);
		} else {
			this.$router.replace('/userinfo');
		}

	},

	async getCompanyName() {
		var telReg = /^[1][3,4,5,7,8][0-9]{9}$/;
		if(!this.loginForm.tel) {
			this.$message.error('手机号不能为空');
			return false
		} else if(!telReg.test(this.loginForm.tel)) {
			this.$message.error('手机号格式不正确')
			return false;
		}

		let params = {};
		params.tel = this.loginForm.tel;
		params.bos = this.currentType == 'personRegister' ? true : false;
		let res = await this.httptool.post(this.apis.getCompany, params);
		this.companyList = this.tool.trimNull(res.data, []);
		if(this.companyList.length==0){
			this.$message('暂无可选公司！');
		}
		this.hasreturn = true;

	},

	async update() {
		//买家调会员注册，卖家调贸易商注册
		let rule = [{
			name: 'productCategoryIds',
			label: '类别',
			rule: ['validateSelectEmpty_']
		}, {
			name: 'realName',
			label: '企业全称',
			rule: ['validateEmpty_']
		}, {
			name: 'shortName',
			label: '企业简称',
			rule: ['validateEmpty_']
		}, {
			name: 'organCode',
			label: '企业统一社会信用代码',
			rule: ['validateSocialCreditCode']
		}, {
			name: 'contactName',
			label: '法人姓名',
			rule: ['validateEmpty_']
		}, {
			name: 'idNo',
			label: '法人身份证',
			rule: ['validateID_']
		}, {
			name: 'contactTel',
			label: '联系人电话',
			rule: ['validateTel_']
		}, {
			name: 'addressPre',
			label: '地址',
			rule: ['validateSelectEmpty_']
		}, {
			name: 'addressDetail',
			label: '详细地址',
			rule: ['validateEmpty_']
		}, {
			name: 'businessLicense',
			label: '营业执照',
			rule: ['validateUploadEmpty_']
		}]
		if(!this.validatetool.validate(this, this.params, rule)) {
			return;
		}
		this.params.userName = this.params.realName;
		let res=await this.httptool.post(this.apis.customerPersonApply, this.params);
		this.$message('提交成功，请耐心等待平台审核！');
		this.tool.cleanToken();
		this.$store.commit('resetStore');
		this.$router.push('/price');
		
	},
	async editUserInfo() {
		let rule = [{
			name: 'productCategoryIds',
			label: '类别',
			rule: ['validateSelectEmpty_']
		}, {
			name: 'realName',
			label: '企业全称',
			rule: ['validateEmpty_']
		}, {
			name: 'shortName',
			label: '企业简称',
			rule: ['validateEmpty_']
		}, {
			name: 'organCode',
			label: '企业统一社会信用代码',
			rule: ['validateSocialCreditCode']
		}, {
			name: 'contactName',
			label: '法人姓名',
			rule: ['validateEmpty_']
		}, {
			name: 'idNo',
			label: '法人身份证',
			rule: ['validateID_']
		}, {
			name: 'contactTel',
			label: '联系人电话',
			rule: ['validateTel_']
		}, {
			name: 'addressPre',
			label: '地址',
			rule: ['validateSelectEmpty_']
		}, {
			name: 'addressDetail',
			label: '详细地址',
			rule: ['validateEmpty_']
		}, {
			name: 'businessLicense',
			label: '营业执照',
			rule: ['validateUploadEmpty_']
		}]
		if(!this.validatetool.validate(this, this.params, rule)) {
			return;
		}
//		this.$confirm('修改资料后，后台需要重新审核,确认提交吗？').then(_ => {
			let res = await this.httptool.post(this.apis.editInfo, this.params);
			if(res.code == '131') {
				this.$message('提交成功,请耐心等待后台审核!');
				setTimeout(function() {
					this.tool.cleanToken();
					this.$store.commit('changeValue', {
						name: 'userInfo',
						value: null
					});
					this.$router.replace('/price');
				}, 2000);

			} else {
				this.$message('修改成功！'); //根据后台返的信息判断是否需要重新登录到login界面
				setTimeout(function() {
					this.$router.replace('/userinfo');
				}, 2000);
			}
//		}).catch(_ => {});
	},
	async editHeadImg() {
		let params = {
			headImg: this.modelData.headImg,
			id: this.modelData.id
		}
		let res=await this.httptool.post(this.apis.customerEditCron, params);
		this.getUserInfo();
		
	},
	async getUserInfo() {
		let res = await this.httptool.post(this.apis.getCustomerDetail, {});
		this.modelData = res.data;
		this.$store.commit('changeValue', {
			name: 'userInfo',
			value: res.data
		})
		this.params = res.data;
		this.address = res.data.addressDetail;
		
//		this.category.productCategoryIds = res.data.productCategoryIds.split(',')
		this.oriproductCategoryIds = res.data.productCategoryIdsCN;

	},
	async realNameCut(val) {
		if(val.length > 8) {
			return val.substring(0, 8) + '...';
		} else {
			return val
		}
	},
	async getMsgcount() {
		let res = await this.httptool.post(this.apis.getMsgCount, {});
		this.msgCount = res.data.total
	},
	async resetPassword() {
		let rule = [{
					name: 'tel',
					label: '手机号',
					rule: ['validateTel_']
				},{
					name: 'code',
					label: '验证码',
					rule: ['validateCode']
				}, {
					name: 'pwd',
					label: '密码',
					rule: ['validatePwd']
				}, {
					name: 'confirmPwd',
					label: '确认密码',
					rule: ['validatePwd']
				}]
				
				if(this.validatetool.validate(this, this.loginForm, rule)) {
					if(this.userCurrentType!=0){
						if(!this.loginForm.realName){
							this.$message('请先选择企业');
							return false
						}
					}
					if(this.loginForm.pwd != this.loginForm.confirmPwd) {
						this.$message.error('两次密码输入不一致');
						return false
					}
					let params = {
						phone: this.loginForm.tel,
						code: this.loginForm.code,
						bos:this.loginForm.bos,
					}
					let res=await this.httptool.post(this.apis.forgetPwd,this.loginForm);
						this.$message.success('新密码设置成功！');
						this.$router.push('/login');
					
				}
	}
}